package org.andy.shop.controller;

import org.andy.shop.entity.UserInfo;
import org.andy.shop.service.UserInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: Chen
 * @Date: 2016/4/16 14:50
 * @Email: wirechen@gmail.com
 * @Description:
 */
@Controller
@RequestMapping("/user")
public class UserInfoController {

    private static final Logger LOGGER = Logger.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/showInfo/{userId}")
    public String showUserInfo(Model modelMap, @PathVariable int userId) {
        LOGGER.info("查看用户：" + userId);
        UserInfo userInfo = userInfoService.getById(userId);
        modelMap.addAttribute("userInfo", userInfo);
        return "/user/showInfo";
    }

    @RequestMapping("/upload")
    public String fileUpload() {

        return "/upload/upload";
    }

//    @RequestMapping("/showInfos")
//    @ResponseBody
//    public List<UserInfo> showUserInfos() {
//        LOGGER.info("json返回全部用户的信息");
//        List<UserInfo> userInfos = userInfoService.findAll();
//        return userInfos;
//    }

    @RequestMapping("/showInfos")
    public ResponseEntity<?> showUserInfos() {
        LOGGER.info("json返回全部用户的信息");
        List<UserInfo> userInfos = userInfoService.findAll();
        return new ResponseEntity(userInfos, HttpStatus.OK);
    }

    @RequestMapping(value="/saveUserInfo", method = RequestMethod.POST)
    public ResponseEntity<?> saveUserInfo(@RequestParam("uname") String uname,
                                          @RequestParam("unumber") int unumber,
                                          @RequestParam("uRegisterTime") Date uRegisterTime) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUname(uname);
        userInfo.setUnumber(unumber);
        userInfo.setuRegisterTime(uRegisterTime);
        userInfoService.save(userInfo);
        return new ResponseEntity(true, HttpStatus.OK);
    }

    @RequestMapping(value="/deleteUserInfo", method = RequestMethod.POST)
    public ResponseEntity<?> deleteUserInfo(@RequestParam("id") int id) {
        userInfoService.delete(id);
        return new ResponseEntity("success", HttpStatus.OK);
    }

    @RequestMapping(value="/updateUserInfo", method = RequestMethod.POST)
    public ResponseEntity<?> updateUserInfo(@RequestParam("uname") String uname,
                                            @RequestParam("unumber") int unumber,
                                            @RequestParam("uRegisterTime") Date uRegisterTime,
                                            @RequestParam("id") int id) {
        UserInfo userInfo = userInfoService.getById(id);
        userInfo.setUname(uname);
        userInfo.setUnumber(unumber);
        userInfo.setuRegisterTime(uRegisterTime);
        userInfoService.update(userInfo);
        return new ResponseEntity("success", HttpStatus.OK);
    }

}